package com.iyunwen.datatool.rule.impl;

import com.iyunwen.datatool.model.dbdo.EventDataDO;
import com.iyunwen.datatool.model.vo.EventGroupVO;
import com.iyunwen.datatool.rule.ISignalAttentionRuleEngine;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: Zhu.Guodong
 * @create: 2020-03-17 10:26
 * @version:
 */
@Service
public class SignalAttentionRuleEngineImpl implements ISignalAttentionRuleEngine {


    /**
     * 高等级
     */
    String[] LEVEL_KEY_WORD = {"紧急/故障", "警告"};

    /**
     * 匹配关键词
     */
    String[] MATCH_KEY_REGEX = {
            ".*闭锁.*",
            ".*跳闸.*",
            ".*故障.*",
            ".*ESOF.*",
            ".*异常.*",
            ".*禁止.*",
            ".*切换.*系统.*",
            ".*冗余耗尽.*",
            ".*强制移相.*",
            ".*另一系统正常.*",
            ".*脉冲.*",
            ".*VBE.*",
    };

    String[] EVENT_STATUS = {"产生", "消失"};


    @Override
    public boolean isAttentionSignal(EventDataDO eventDataDO) {
        //事件等级
        boolean contains = isHighLevelSignal(eventDataDO);
        if (contains) {
            for (String match_key_regex : MATCH_KEY_REGEX) {
                //事件描述
                String eventDesc = eventDataDO.getEventDesc();
                //正则匹配
                Pattern p = Pattern.compile(match_key_regex);
                // 获取 matcher 对象
                Matcher m = p.matcher(eventDesc);
                boolean matches = m.matches();
                return matches;
            }
        }

        return false;
    }

    /**
     * 是否是高等级事件
     * @param eventDataDO
     * @return
     */
    private boolean isHighLevelSignal(EventDataDO eventDataDO){
        //事件等级
        String eventLevel = eventDataDO.getEventLevel();
        List<String> highLevels = Arrays.asList(LEVEL_KEY_WORD);
        return highLevels.contains(eventLevel);
    }

    @Override
    public boolean isRelateAttentionSignal(EventDataDO indexEventData, EventDataDO compareEventData) {
        return false;
    }

    @Override
    public EventGroupVO getGroupByEventDataAndRangeData(EventDataDO eventData, List<EventDataDO> rangeDataList) {
        EventGroupVO eventGroupVO = new EventGroupVO();
        List<EventDataDO> groupEventDataList = new ArrayList<>();
        String eventStatus = eventData.getEventStatus();

        //产生消失状态的记录
        List<String> eventStatusRecord = Arrays.asList(EVENT_STATUS);
        boolean eventStatusFlag = false;
        String compareStatus = "";
        if (eventStatusRecord.contains(eventStatus)) {
            eventStatusFlag = true;
            int index = eventStatusRecord.indexOf(eventStatus);
            int compareIndex = 0 == index ? 1 : 0;
            compareStatus = EVENT_STATUS[compareIndex];
        }
        //是否是关联的事件
        for (EventDataDO itemEvent : rangeDataList) {
            boolean highLevelSignal = isHighLevelSignal(itemEvent);
            if (highLevelSignal) {
                groupEventDataList.add(itemEvent);
            }else if (eventStatusFlag && compareStatus.equals(itemEvent.getEventStatus()) && eventData.getEventDesc().equals(itemEvent.getEventDesc()) ) {
                groupEventDataList.add(itemEvent);
            }
        }

        //关联数据
        eventGroupVO.setEventData(groupEventDataList);
        return eventGroupVO;
    }

}
